//index.js
//获取应用实例
var http = require("../../utils/http.js");
var config = require("../../utils/config.js");
const app = getApp()

Page({
  data: {
    indicatorDots: true,
    indicatorColor: '#d1e5fb',
    indicatorActiveColor: '#1b7dec',
    autoplay: true,
    interval: 3000,
    duration: 1000,
    indexImgs: [],
    seq: 0,
    news: [],
    taglist: [],
    sts: 0,
    controls: false,
  },
  // videoPlay: function () {
  //   console.log("开始播放")
  //   var videoplay = wx.createVideoContext("video");
  //   videoplay.play()
  //   this.setData({
  //     controls: true,
  //   })
  // },
  playVideo: function () {
    console.log("开始播放") 
    this.setData({
      autoplay: false,
    })
  },
  pauseVideo:function(){
    console.log("暂停播放") 
    this.setData({
      autoplay: true,
    })
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.getAllData();
  },

  // 页面滚动到指定位置指定元素固定在顶部
  onPageScroll: function (e) { //监听页面滚动
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  tobarner: function (e) {
    return;
    wx.navigateTo({
      url: '/pages/web-view/web-view',
    })
  },
  toProdPage: function (e) {
    var prodid = e.currentTarget.dataset.prodid;
    if (prodid) {
      wx.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodid,
      })
    }
  },


  toCouponCenter: function () {
    wx.showToast({
      icon: "none",
      title: '该功能未开源'
    })
  },

  // 跳转搜索页
  toSearchPage: function () {
    wx.navigateTo({
      url: '/pages/search-page/search-page',
    })
  },

  //跳转商品活动页面
  toClassifyPage: function (e) {
    var url = '/pages/prod-classify/prod-classify?sts=' + e.currentTarget.dataset.sts;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    console.log(title, id);
    if (id) {
      url += "&tagid=" + id + "&title=" + title;
    }
    wx.navigateTo({
      url: url
    })
  },
  //跳转限时特惠页面
  toLimitedTimeOffer: function (e) {
    wx.showToast({
      icon: "none",
      title: '敬请期待'
    })
  },
  //跳转公告列表页面
  onNewsPage: function (e) {
    let type = e.currentTarget.dataset.type;
    let url = '/pages/recent-news/recent-news'
    if (type == 1) {
      url = '/pages/recent-news2/recent-news'
    }
    wx.navigateTo({
      url: url,
    })
  },
  toNewDetail(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    let title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/news-plan/news-plan?id=' + id + "&title=" + title,
    })
  },


  onShow: function () { },
  getAllData() {
    http.getCartCount(); //重新计算购物车总数量
    this.getIndexImgs();
    this.getNoticeList();
    this.getTag();
  },
  //加载轮播图
  getIndexImgs() {
    //加载轮播图
    var params = {
      url: "/indexImgs",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          indexImgs: res,
          seq: res
        });
      }
    };
    http.request(params);
  },
  getNoticeList() {
    // 加载公告
    var params = {
      url: "/shop/notice/topNoticeList",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          news: res,
        });
      }
    };
    http.request(params);
    
  },

  /**
   * 加入购物车
   */
  addToCart(e) {
    const prodId = e.currentTarget.dataset.prodid
    const ths = this
    wx.showLoading();
    var params = {
      url: "/prod/prodInfo",
      method: "GET",
      data: {
        prodId
      },
      callBack: (res) => {
        var params = {
          url: "/p/shopCart/changeItem",
          method: "POST",
          data: {
            basketId: 0,
            count: 1,
            prodId: res.prodId,
            shopId: res.shopId,
            skuId: res.skuList[0].skuId
          },
          callBack: function (res) {
            ths.setData({
              totalCartNum: ths.data.totalCartNum + ths.data.prodNum
            });
            wx.hideLoading();
            http.getCartCount(); //重新计算购物车总数量
            wx.showToast({
              title: "加入购物车成功",
              icon: "none"
            })
          }
        };
        http.request(params);
      }
    };
    http.request(params);
  },


  // 加载商品标题分组列表
  getTag() {
    var params = {
      url: "/prod/tag/prodTagList",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          taglist: res,
        });
        for (var i = 0; i < res.length; i++) {
          this.getTagProd(res[i].id, i);
        }
      }
    };
    http.request(params);
  },

  getTagProd(id, index) {
    var param = {
      url: "/prod/prodListByTagId",
      method: "GET",
      data: {
        tagId: id,
        size: 6
      },
      callBack: (res) => {
        var taglist = this.data.taglist;
        taglist[index].prods = res.records

        this.setData({
          taglist: taglist,
        });
      }
    };
    http.request(param);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {
  //     wx.request({
  //       url: '',
  //       data: {},
  //       method: 'GET',
  //       success: function (res) { },
  //       fail: function (res) { },
  //       complete: function (res) {
  //         wx.stopPullDownRefresh();
  //       }
  //     })
  // },

  onPullDownRefresh: function () {

    // wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    var ths = this;
    setTimeout(function () {

      ths.getAllData();

      // wx.hideNavigationBarLoading() //完成停止加载

      wx.stopPullDownRefresh() //停止下拉刷新

    }, 100);

  },

  /**
   * 跳转至商品详情
   */
  showProdInfo: function (e) {
    let relation = e.currentTarget.dataset.relation;
    if (relation) {
      wx.navigateTo({
        url: 'pages/prod/prod',
      })
    }
  }
})